package com.mall.manager.sdkclick.controller;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;
import com.mall.common.entity.SdkClick;
import com.mall.manager.sdkclick.service.SdkClickService;
import com.mall.untils.DateUtil;
/**
 * @按钮点击统计
 * @author liugb
 * @date 2016 7 12
 */
@Controller
@RequestMapping("/manager")
public class SdkClickController {
	Logger logger = LoggerFactory.getLogger(SdkClickController.class);
	@Resource
	SdkClickService sdkClickService;	
    /**
     * @按点击类型显示柱状图
     * @param request
     * @return
     */
	@RequestMapping(value = "/manager_sdkClick_list")
	public String manager_sdkClick_list(HttpServletRequest request,ModelMap modelMap) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		Calendar cal=Calendar.getInstance();
		cal.setTime(new Date()); 
		int week=cal.get(Calendar.DAY_OF_WEEK)-1;    //星期一时week=1,week=0是星期天 
		//当天的数据
		String time  = DateUtil.getTodayString();
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		List<Map<String, Object>> rs = sdkClickService.manager_sdkClick_list(pwsd,time);
		SdkClick click = new SdkClick(0, 0, 0, 0, 0, 0, 0, 0, 0);
		if(rs!=null && rs.size()>0){
			for(int i=0;i<rs.size();i++){
				click = new SdkClick();
				Map<String,Object> m = rs.get(i);
				if(MapUtils.getString(m,"data_type").equals("直接访问")){
					click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
					click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
					click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
					click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
					click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("必应")){
					click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("其他")){
					click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
				}
				
			}
			
		}	
		if(week == 1){
			modelMap.addAttribute("Week1", click);
		}else if(week == 2){
			modelMap.addAttribute("Week2", click);
		}else if(week == 3){
			modelMap.addAttribute("Week3", click);
		}else if(week == 4){
			modelMap.addAttribute("Week4", click);
		}else if(week == 5){
			modelMap.addAttribute("Week5", click);
		}else if(week == 6){
			modelMap.addAttribute("Week6", click);
		}else if(week == 0){
			modelMap.addAttribute("Week0", click);
		}
		//前一天查询
		String time1  = DateUtil.getBackForDate(1);
		List<Map<String, Object>> rs1 = sdkClickService.manager_sdkClick_list(pwsd,time1);
		if(rs1!=null && rs1.size()>0){
			click = new SdkClick();
			for(int i=0;i<rs1.size();i++){
				Map<String,Object> m = rs1.get(i);
				if(MapUtils.getString(m,"data_type").equals("直接访问")){
					click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
					click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
					click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
					click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
					click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("必应")){
					click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("其他")){
					click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
				}
				
			}
		   
			
		}
		int week1 = 0;
		try {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE,   -1);
			c.setTime(c.getTime());
			cal.setTime(cal.getTime());
			week1 = c.get(Calendar.DAY_OF_WEEK) - 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(week1 == 1){
			modelMap.addAttribute("Week1", click);
		}else if(week1 == 2){
			modelMap.addAttribute("Week2", click);
		}else if(week1 == 3){
			modelMap.addAttribute("Week3", click);
		}else if(week1 == 4){
			modelMap.addAttribute("Week4", click);
		}else if(week1 == 5){
			modelMap.addAttribute("Week5", click);
		}else if(week1 == 6){
			modelMap.addAttribute("Week6", click);
		}else if(week1 == 0){
			modelMap.addAttribute("Week0", click);
		}
		//前两天查询
		String time2  = DateUtil.getBackForDate(2);
		List<Map<String, Object>> rs2 = sdkClickService.manager_sdkClick_list(pwsd,time2);
		if(rs2!=null && rs2.size()>0){
			click = new SdkClick();
			for(int i=0;i<rs2.size();i++){
				Map<String,Object> m = rs2.get(i);
				if(MapUtils.getString(m,"data_type").equals("直接访问")){
					click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
					click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
					click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
					click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
					click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("必应")){
					click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("其他")){
					click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
				}
				
			}
			
		}
		int week2 = 0;
		try {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE,   -2);
			c.setTime(c.getTime());
			week2 = c.get(Calendar.DAY_OF_WEEK) - 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(week2 == 1){
			modelMap.addAttribute("Week1", click);
		}else if(week2 == 2){
			modelMap.addAttribute("Week2", click);
		}else if(week2 == 3){
			modelMap.addAttribute("Week3", click);
		}else if(week2 == 4){
			modelMap.addAttribute("Week4", click);
		}else if(week2 == 5){
			modelMap.addAttribute("Week5", click);
		}else if(week2 == 6){
			modelMap.addAttribute("Week6", click);
		}else if(week2 == 0){
			modelMap.addAttribute("Week0", click);
		}
		//前三天查询
		String time3  = DateUtil.getBackForDate(3);
		List<Map<String, Object>> rs3 = sdkClickService.manager_sdkClick_list(pwsd,time3);
		if(rs3!=null && rs3.size()>0){
			click = new SdkClick();
			for(int i=0;i<rs3.size();i++){
				Map<String,Object> m = rs3.get(i);
				if(MapUtils.getString(m,"data_type").equals("直接访问")){
					click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
					click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
					click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
					click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
					click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("必应")){
					click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
				}else if(MapUtils.getString(m,"data_type").equals("其他")){
					click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
				}
				
			}
			
		}		
		int week3 = 0;
		try {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE,   -3);
			c.setTime(c.getTime());
			week3 = c.get(Calendar.DAY_OF_WEEK) - 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(week3 == 1){
			modelMap.addAttribute("Week1", click);
		}else if(week3 == 2){
			modelMap.addAttribute("Week2", click);
		}else if(week3 == 3){
			modelMap.addAttribute("Week3", click);
		}else if(week3 == 4){
			modelMap.addAttribute("Week4", click);
		}else if(week3 == 5){
			modelMap.addAttribute("Week5", click);
		}else if(week3 == 6){
			modelMap.addAttribute("Week6", click);
		}else if(week3 == 0){
			modelMap.addAttribute("Week0", click);
		}
		//前三天查询
				String time4  = DateUtil.getBackForDate(4);
				List<Map<String, Object>> rs4 = sdkClickService.manager_sdkClick_list(pwsd,time4);
				if(rs4!=null && rs4.size()>0){
					click = new SdkClick();
					for(int i=0;i<rs4.size();i++){
						Map<String,Object> m = rs4.get(i);
						if(MapUtils.getString(m,"data_type").equals("直接访问")){
							click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
							click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
							click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
							click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
							click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("必应")){
							click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("其他")){
							click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
						}
						
					}
				}	
				int week4 = 0;
				try {
					Calendar c = Calendar.getInstance();
					c.add(Calendar.DATE,   -4);
					c.setTime(c.getTime());
					week4 = c.get(Calendar.DAY_OF_WEEK) - 1;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(week4 == 1){
					modelMap.addAttribute("Week1", click);
				}else if(week4 == 2){
					modelMap.addAttribute("Week2", click);
				}else if(week4 == 3){
					modelMap.addAttribute("Week3", click);
				}else if(week4 == 4){
					modelMap.addAttribute("Week4", click);
				}else if(week4 == 5){
					modelMap.addAttribute("Week5", click);
				}else if(week4 == 6){
					modelMap.addAttribute("Week6", click);
				}else if(week4 == 0){
					modelMap.addAttribute("Week0", click);
				}
				
				//前三天查询
				String time5  = DateUtil.getBackForDate(5);
				List<Map<String, Object>> rs5 = sdkClickService.manager_sdkClick_list(pwsd,time5);
				if(rs5!=null && rs5.size()>0){
					click = new SdkClick();
					for(int i=0;i<rs5.size();i++){
						Map<String,Object> m = rs5.get(i);
						if(MapUtils.getString(m,"data_type").equals("直接访问")){
							click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
							click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
							click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
							click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
							click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("必应")){
							click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("其他")){
							click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
						}
						
					}
					
				}	
				int week5 = 0;
				try {
					Calendar c = Calendar.getInstance();
					c.add(Calendar.DATE,   -5);
					c.setTime(c.getTime());
					week5 = c.get(Calendar.DAY_OF_WEEK) - 1;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(week5 == 1){
					modelMap.addAttribute("Week1", click);
				}else if(week5 == 2){
					modelMap.addAttribute("Week2", click);
				}else if(week5 == 3){
					modelMap.addAttribute("Week3", click);
				}else if(week5 == 4){
					modelMap.addAttribute("Week4", click);
				}else if(week5 == 5){
					modelMap.addAttribute("Week5", click);
				}else if(week5 == 6){
					modelMap.addAttribute("Week6", click);
				}else if(week5 == 0){
					modelMap.addAttribute("Week0", click);
				}
				//前三天查询
				String time6  = DateUtil.getBackForDate(6);
				List<Map<String, Object>> rs6 = sdkClickService.manager_sdkClick_list(pwsd,time6);
				if(rs6!=null && rs6.size()>0){
					click = new SdkClick();
					for(int i=0;i<rs6.size();i++){
						Map<String,Object> m = rs6.get(i);
						if(MapUtils.getString(m,"data_type").equals("直接访问")){
							click.setZhClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("邮件营销")){
							click.setEmailClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("联盟广告")){
							click.setLianmClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("视频广告")){
							click.setViewClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("搜索引擎")){
							click.setSeachClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("必应")){
							click.setBiyClickCount(MapUtils.getInteger(m,"click_count"));
						}else if(MapUtils.getString(m,"data_type").equals("其他")){
							click.setOtherClickCount(MapUtils.getInteger(m,"click_count"));
						}
						
					}
					
				}					
				int week6 = 0;
				try {
					Calendar c = Calendar.getInstance();
					c.add(Calendar.DATE,   -6);
					c.setTime(c.getTime());
					week6 = c.get(Calendar.DAY_OF_WEEK) - 1;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(week6 == 1){
					modelMap.addAttribute("Week1", click);
				}else if(week6 == 2){
					modelMap.addAttribute("Week2", click);
				}else if(week6 == 3){
					modelMap.addAttribute("Week3", click);
				}else if(week6 == 4){
					modelMap.addAttribute("Week4", click);
				}else if(week6 == 5){
					modelMap.addAttribute("Week5", click);
				}else if(week6 == 6){
					modelMap.addAttribute("Week6", click);
				}else if(week6 == 0){
					modelMap.addAttribute("Week0", click);
				}
		return "sdkclick/click_statistics";
	}
}
